#ifndef PROTOCOL_H_
#define PROTOCOL_H_





#define AP_VERSION 0X0001



#pragma pack(1)

//定时数据
typedef struct _etc_4g_timer_data
{
	//系统状态
	struct _etc_sys_status
	{
		uint32_t number;    //流水号 递增
		uint32_t sys_time; //系统时间 ，传送时候填写
		uint32_t data_time;  //数据时间
		float gps_n_e[2];
	}struct_etc_sys_status;	
	uint8_t sensor_num;
	//sensor状态和数据（定时）
	struct _etc_data_and_status
	{
		uint16_t sensor_id;   
		int8_t rssi;        //平均信号强度		
		uint16_t sensor_software_version;  //sensor版本号
		int8_t sensor_rev_rssi;
		uint8_t voltage;
		uint8_t mode;		
		uint16_t packet_count;     //包总数
		uint16_t packet_lost_count;   //丢包数
		uint16_t packet_resend_count;  //重传数		
		uint8_t net_out_times; //掉网次数
		uint16_t net_out_time; //掉网时间 累积		
		
		uint16_t car_count; //过车统计
		uint16_t car_type_count[5]; //分车型过车统计		
		uint8_t on_times;    //占有率
		uint8_t car_head_time; //车头时距
		uint8_t car_to_car_distance; //车间距
	}struct_etc_data_and_status[SENSOR_MAX_COUNT];	
}struct_etc_4g_timer_data;


//sensor 升级状态包上报
typedef struct _etc_sensor_updata_status
{
	uint32_t sys_time; //系统时间 ，传送时候填写
	struct{
	uint16_t sensor_id;
	int8_t updata_percent; 
	}sensor_updata_status[1];
}struct_etc_sensor_updata_status;


//sensor 实时过车数据
typedef struct _etc_sensor_car_data
{
	uint16_t sensor_id;
	uint32_t number; //流水号
	uint32_t sys_time; //系统时间 ，传送时候填写
	uint32_t data_time;  //数据时间	
	uint8_t position;  //位置 1-7 IO
	uint32_t on_time;    //压站时间
}struct_etc_sensor_car_data;


//sensor 车辆ON状态上报
typedef struct _etc_sensor_on_event
{
	uint16_t sensor_id;
	uint32_t number; //流水号
	uint32_t sys_time; //系统时间 ，传送时候填写
	uint32_t data_time;  //数据时间	
	uint8_t position;  //位置 1-7 IO
	uint8_t event_packet_seq;
	uint8_t resend_times;
	uint16_t event;
}struct_etc_sensor_on_off_event;


typedef struct _gprs_4g_packet
{
	uint32_t head;
	#define PROTOCOL_HEAD      0X55AA55AA
	uint32_t ap_id;
	uint16_t len;
	uint8_t cmd1;	
	uint8_t cmd2;		
}struct_gprs_4g_packet_head;


typedef struct _gprs_4g_firmware_data
{
	uint16_t packet_count;         //固件总长度
	uint16_t packet_seq;     //固件当前包的长度
	uint8_t data[1];
}struct_gprs_4g_firmware_data;

typedef struct _gprs_4g_updata_sensor_data
{
	uint8_t sensor_count;         //固件总长度
	uint16_t sensor_id[1];     //固件当前包的长度
}struct_gprs_4g_updata_sensor_data;

typedef struct _etc_4g_server_protocol
{
	struct_gprs_4g_packet_head packet_head;
	uint8_t data[1];
}struct_etc_4g_server_protocol;


typedef struct _firmware_ack
{
	struct_gprs_4g_packet_head packet_head;
	uint16_t packet_seq;     //固件当前包的长度
	uint16_t crc;
}struct_firmware_ack;

typedef struct _to_4g_ack_no_data
{
	struct_gprs_4g_packet_head packet_head;
	uint16_t crc;
}struct_to_4g_ack_no_data;

typedef struct _flash_head_crc{

	unsigned int head;
	unsigned int crc;

}struct_flash_head_crc;

typedef struct _FW_HEADER_t_5xx
{
 uint32_t  ulId;    // 0x00 - ??'Header'??.
  #define FW_HEADER_ID   0xDADA10AFul
 uint32_t  ulCheckSum;   // 0x04 - ???, ??'update.c'??'UfCalcCheckSum'.
 uint16_t  uiSize;    // 0x08 - sizeof( FW_HEADER_t ).
 uint16_t  uiFwSize;   // 0x0A - ???????.
 uint16_t  uiBuildNr;   // 0x0C - ??? ????'Build'??.
 uint16_t  auiVector[64u];  // 0x0E - ???????????.
 //u16_t  uiFwAddr;   // 0x2E - ???????.
 uint32_t  uiFwAddr;   // 0x2E - ???????.
 uint16_t  uiHwVer;   // 0x30 - ???????????.??????
 uint16_t  ucCfgComb;   // 0x32 - ??? configuration combination.
         //   ??????????:
         //   0x03: ???.
         //   0x07: ???.
         //   0x08: ????'Stop Bar'??.
 uint16_t  uiFwVer;   // 0x34 - ????.
	uint16_t d[8];
} FW_HEADER_t_5xx;



#define FLASH_PARAM1      0x08004000
#define FLASH_PARAM2      0x08008000
#define FLASH_PARAM3      0x0800c000
#define FLASH_PARAM4      0x08010000

#define FIRM_HEAD_ERROR 0XFFFE
#define FIRM_LINES_ERROR 0XFFFD
#define FIRM_AP     1
#define FIRM_SENSOR 2


#define FLASH_SENSOR_FIRMWARE_BEGIN   0x080a0000     //sensor固件起始地址
#define FLASH_AP_FIRMWARE_BEGIN       0x08060008     //ap固件起始地址
#define FLASH_AP_FIRMWARE_HEAD       0x08060000     //ap固件起始地址

#pragma pack()


int8_t save_sys_param_to_flash(uint32_t flash_addr,uint8_t sector_num);

void rev_4g_server_ap_firmware(int32_t whitch_client,uint8_t *pdata, uint16_t p_count,uint16_t p_seq,int16_t len);

void send_4g_firmware_ack(int32_t whitch_client,uint16_t p_count,uint16_t now_seq,void*file_name);

void read_firmware_sensor_head();

void rev_4g_updata_sensor_list(int32_t whitch_client,struct_gprs_4g_updata_sensor_data *pdata);


#endif
